package com.biz.primus.ms.homepage.dao.ro;

import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.annotation.RoSortedSet;
import com.biz.primus.base.redis.bean.BaseRedisObject;
import com.biz.primus.homepage.enums.TimeRangePolicy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.apache.commons.collections.ComparatorUtils;

import java.util.Comparator;
import java.util.Date;

/**
 * 区域首页里的功能模块 Created by defei on 2/13/17.
 */
@Data
@SuppressWarnings({ "rawtypes", "unchecked" })
@Ro(key = "soa:cover:programRo")
@RoSortedSet(key = "list", score = "createTimestamp")
public class ProgramRo extends BaseRedisObject<Long> implements Comparable<ProgramRo> {

	private String coverId;

	private Long parentId;

	private String sourceId;

	private String dataJson;

	private String description;

	private String name;

	private CommonStatus status = CommonStatus.ENABLE;

	private TimeRangePolicy timeRangePolicy = TimeRangePolicy.INHERIT;

	/**
	 * 开始时间
	 */
	private Date startTime;

	/**
	 * 结束时间
	 */
	private Date endTime;

	@JsonIgnore
	transient private ProgramRo parent;

	/**
	 * 是否在有效时间内
	 */
	@JsonIgnore
	public boolean atTheRightTime() {

		if (timeRangePolicy == TimeRangePolicy.INHERIT && parent != null) {
			return (parent.atTheRightTime());
		} else {
			long currentTimeMillis = System.currentTimeMillis();
			return startTime != null && endTime != null && currentTimeMillis >= startTime.getTime()
					&& System.currentTimeMillis() <= endTime.getTime();
		}
	}

	@Override
	public int compareTo(ProgramRo o) {
		if (o == null) {
			return 1;
		} else {
			Comparator nullLowComparator = ComparatorUtils.nullLowComparator(null);
			int compare = nullLowComparator.compare(getId(), o.getId());
			return compare == 0 ? nullLowComparator.compare(o.getName(), getName()) : compare;
		}
	}


}
